<?php
include_once($_SERVER["DOCUMENT_ROOT"] . "/model/base_function.php");

function model_get_product_list($type, $page, $list_per_page)
{
	$product_list = array();
	$connection = connect_to_database();

	$query_string = "select * from product";
	if ($type)
	{
		$query_string = $query_string . " WHERE type_id = " . $type;
	}

	$query_string = $query_string . " ORDER BY id";
	$query_string = $query_string . " LIMIT ". ($page - 1) * $list_per_page . ", " . $list_per_page;

	$result = mysql_query($query_string, $connection);

	while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
		$product = $row;
		$product["detail_view_url"] = "details.php?id=" . $product["id"]; 
		$product_list[] = $product;
	}
	
	mysql_close($connection);
	return $product_list;
}

function model_get_page_count($type, $list_per_page)
{
	$connection = connect_to_database();

	$query_string = "select count(*) from product";
	if ($type)
	{
		$query_string = $query_string . " WHERE type_id = " . $type;
	}
	$result = mysql_query($query_string, $connection);
	$row = mysql_fetch_row($result);
	$result_count = $row[0];

	$page_count = $result_count / $list_per_page;
	if ($result_count % $list_per_page > 0)
	{
		$page_count = $page_count + 1;
	}
	return $page_count;
}

function model_get_product_details($product_id)
{
	$product_details = array();
	$connection = connect_to_database();

	$query_string = "select * from product where id = " . $product_id;
	$result = mysql_query($query_string, $connection);
	if (!$result)
	{
		return;
	}

	$product_details = mysql_fetch_array($result, MYSQL_ASSOC);
	mysql_free_result($result);

	mysql_close($connection);
	return $product_details;
}

function model_upate_product_details($product_id)
{
	$product_details = array();
	$connection = connect_to_database();

	$query_string =
	"UPDATE product SET click_count = click_count + 1 WHERE id = " . $product_id;
	if (mysql_query($query_string, $connection) == true)
	{
		mysql_close($connection);
		return true;
	}
	else
	{
		mysql_close($connection);
		return false;
	}
}

function model_insert_product($product)
{
	$connection = connect_to_database();

	// sql for upload product
	$query_string =
	"INSERT
		INTO product
		SET
			name = \"". mysql_real_escape_string($product["name"], $connection)  ."\",
			big_image = \"". mysql_real_escape_string($product["big_image"], $connection) ."\",
			small_image = \"". mysql_real_escape_string($product["small_image"], $connection) ."\" ,
			type_id = " . mysql_real_escape_string($product["type"], $connection)  . ",
			model = \"" . mysql_real_escape_string($product["model"], $connection) . "\",
			specification = \"" . mysql_real_escape_string($product["specification"], $connection) . "\",
			market_price = " . mysql_real_escape_string($product["market_price"], $connection) . ",
			promotional_price = " . mysql_real_escape_string($product["promotional_price"], $connection) . ",
			intro = \"" . mysql_real_escape_string($product["intro"], $connection) . "\",
			details = \"" . mysql_real_escape_string($product["details"], $connection) . "\",
			publish_time = NOW();";

	$result = mysql_query($query_string, $connection);
	if ($result == NULL)
	{
		print(mysql_error());
		mysql_close($connection);
		return false;
	}

	mysql_close($connection);
	return true;
}

function model_delete_product($id)
{
	$product_details = array();
	$connection = connect_to_database();

	$query_string = "DELETE FROM product where id = " . $id;
	if (mysql_query($query_string, $connection) == true)
	{
		mysql_close($connection);
		return true;
	}
	else
	{
		mysql_close($connection);
		return false;
	}
}

function model_update_product($id, $product)
{
	$connection = connect_to_database();

	// sql for upload product
	$query_string =
	"UPDATE product
		SET
			name = \"". mysql_real_escape_string($product["name"], $connection)  ."\",
			big_image = \"". mysql_real_escape_string($product["big_image"], $connection) ."\",
			small_image = \"". mysql_real_escape_string($product["small_image"], $connection) ."\" ,
			type_id = " . mysql_real_escape_string($product["type"], $connection)  . ",
			model = \"" . mysql_real_escape_string($product["model"], $connection) . "\",
			specification = \"" . mysql_real_escape_string($product["specification"], $connection) . "\",
			market_price = " . mysql_real_escape_string($product["market_price"], $connection) . ",
			promotional_price = " . mysql_real_escape_string($product["promotional_price"], $connection) . ",
			intro = \"" . mysql_real_escape_string($product["intro"], $connection) . "\",
			details = \"" . mysql_real_escape_string($product["details"], $connection) . "\",
			publish_time = NOW()
		WHERE id = " . $product["id"];

	$result = mysql_query($query_string, $connection);
	if ($result == NULL)
	{
		print(mysql_error());
		mysql_close($connection);
		return false;
	}

	mysql_close($connection);
	return true;
}
?>
